/**
 * JMPlurk, a Java Mobile client for Plurk
 * Copyright (C) 2009 Petra Barus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.maleskoding.jmplurk.ui;

import com.maleskoding.jmplurk.plurkapi.PlurkException;
import com.maleskoding.jmplurk.ui.components.PlurkViewComponent;
import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import java.io.IOException;
import org.json.me.JSONException;

/**
 *
 * @author Petra Barus
 */
public class ViewPlurkForm extends Form implements ActionListener {

    private PlurkViewComponent plurkViewComponent;
    private Form parent;
    private Command backCommand;
    private Command responseCommand;
    private Command muteCommand;
    private ResponseForm responseForm;
    //
    //
    private String plurk_id;

    /**
     * 
     */
    public ViewPlurkForm(Form parent) {
        super("View Plurk");
        this.parent = parent;
        init();
        this.addCommandListener(this);
    }

    /**
     *
     */
    public void init() {

        plurkViewComponent = new PlurkViewComponent();
        this.addComponent(plurkViewComponent);
        //
        backCommand = new Command("Back");
        muteCommand = new Command("Mute");
        responseCommand = new Command("Response");

        this.addCommand(backCommand);
        this.addCommand(muteCommand);
        this.addCommand(responseCommand);
        responseForm = new ResponseForm(this);
    }

    /**
     *
     * @param plurk_id
     */
    public void loadPlurk(String plurk_id) {
        System.err.println("Loading Plurk #" + plurk_id);
        this.plurk_id = plurk_id;
        try {
            plurkViewComponent.loadPlurk(plurk_id);
        } catch (PlurkException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    public void reload() {
        try {
            plurkViewComponent.loadPlurk(plurk_id);
        } catch (PlurkException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    /**
     *
     */
    private void mutePlurk() {
        System.err.println("Muting Plurk #" + plurk_id);
    }

    /**
     *
     * @param arg0
     */
    public void actionPerformed(ActionEvent arg0) {
        Command c = arg0.getCommand();
        if (c == backCommand) {
            parent.show();
        } else if (c == responseCommand) {
            System.err.println("Response");
            responseForm.show(plurk_id);
        } else if (c == muteCommand) {
            mutePlurk();
        }
    }
}
